package com.zhonghan.recharge.task;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.common.wechat.api.TemplateMsgApi;
import com.zhonghan.common.wechat.api.UserApi;
import com.zhonghan.common.wechat.bean.result.BaseResult;
import com.zhonghan.common.wechat.bean.template.TemplateMsg;
import com.zhonghan.common.wechat.bean.user.BaseUserInfo;
import com.zhonghan.recharge.dao.ZhGascardOrderDao;
import com.zhonghan.recharge.pojo.ZhGascardOrder;
import com.zhonghan.recharge.service.JiSuRechargeService;

@Component
public class JiSuOrderTask {
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private JiSuRechargeService jiSuRechargeService;
	@Autowired
	private ZhGascardOrderDao orderDao;
	@Value("${wechat.DOMAIN}")
	private String DOMAIN;
	
	 /**
     * 第一个定时器测试方法
     */
    public void testJob(){
        System.out.println("job  .... ");
        /**
         * 遍历jisumap中的数据
         */
        Iterator<String> iterator = TaskList.JISUTASK.keySet().iterator();
        while(iterator.hasNext()) {
        	String orderno = iterator.next();
        	String str = TaskList.JISUTASK.get(orderno);
        	String[] strs = str.split(";");
        	String outOrderno = strs[0];
        	Result result = jiSuRechargeService.changeMyOrderRechargeStatusII(orderno, strs[0],strs[1]);
        	
        	String orderStatus = "";
        	
        	if(result != null && "1".equals(result.getCode())) {
        		orderStatus = "油卡充值成功!";
        		iterator.remove(); // 移除订单
        		
        	}else if(result != null && "2".equals(result.getCode())){
        		
        		orderStatus = "油卡充值失败!";
        		iterator.remove(); // 移除订单
        	}
        	
    		if(StringUtils.isNotEmpty(outOrderno) && StringUtils.isNotEmpty(orderStatus)){
    			
    			ZhGascardOrder order = orderDao.selectZhGascardOrderByOrderNo(outOrderno);
    			String openId = order.getOpenid();
    			
    			if(StringUtils.isNotEmpty(openId)){
    				// 准备推送消息给用户
    				BaseUserInfo baseUserInfo = UserApi.getUserInfo(TaskList.Wechat_Access_Token, openId, "zh_CN");
    				String nickname = baseUserInfo.getNickname();
    				
    				TemplateMsg templateMsg = TemplateMsg.New()
    						.setTouser(openId)
    						.setTemplate_id("o6Uo64TfQTGtC_jcNhkKm3dPqVc8p9h21vB022QhcJA")
    						.setTopcolor("#743A3A")
    						.setUrl( DOMAIN + "/recharge/oil/card/rechargeResult?orderNo=" + order.getOrderNo())
    						.add("first", nickname, "#743A3A")
    						.add("OrderSn", outOrderno, "#0000FF")
    						.add("OrderStatus", orderStatus, "#0000FF")
    						.add("remark", "点击详情查看订单", "#008000");
    				
    				String templateMsgJson = JSON.toJSONString(templateMsg);
    				
    				BaseResult baseResult = TemplateMsgApi.sendMessage(TaskList.Wechat_Access_Token, templateMsgJson);
    				logger.info("油卡充值订单状态消息推送结果: " + baseResult);
    				if(baseResult != null && baseResult.getErrcode().equals("0")){
    					logger.info("油卡充值订单状态消息推送成功!");
    				}
    				
    			}
    		}
        }
        
       
        
        
    }
}
